package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.OrderCommentBO;
import com.cskaoyan.bean.bo.OrderListWxBO;
import com.cskaoyan.bean.bo.OrderSubmitWxBO;
import com.cskaoyan.bean.pojo.MarketComment;
import com.cskaoyan.bean.pojo.MarketOrderGoods;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.OrderDetailWxVO;
import com.cskaoyan.bean.vo.OrderListVO;
import com.cskaoyan.bean.vo.OrderListWxVO;
import com.cskaoyan.bean.vo.OrderSubmitWxVO;
import com.cskaoyan.service.OrderWxService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.prefs.BackingStoreException;

/**
 * 微信订单模块
 *
 * @author chenxu
 * @since 2022/04/08 21:38
 */

@RestController
@RequestMapping("wx/order")
public class OrderController {

   @Autowired
   OrderWxService orderService;


   //提交订单
   @PostMapping("submit")
   public BaseRespVo submit( @RequestBody  OrderSubmitWxBO orderSubmitBO){
       Subject subject = SecurityUtils.getSubject();
       MarketUser marketUser = (MarketUser) subject.getPrincipal();

       OrderSubmitWxVO submitWxVO = orderService.submit(marketUser.getId(),orderSubmitBO);
       if (submitWxVO != null){
           return BaseRespVo.ok(submitWxVO);
       }
       return BaseRespVo.fail(404,"返回参数不对哦！");
   }

   //订单预付款
    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody OrderSubmitWxBO orderSubmitWxBO){

       Subject subject = SecurityUtils.getSubject();
       MarketUser marketUser = (MarketUser) subject.getPrincipal();

       int num = orderService.prepay(marketUser.getId(),orderSubmitWxBO.getOrderId());

       return BaseRespVo.fail();
    }

    //查看订单信息
    @RequestMapping("list")
    public BaseRespVo list(OrderListWxBO orderListWxBO){
       Subject subject = SecurityUtils.getSubject();
       MarketUser marketUser = (MarketUser) subject.getPrincipal();

       OrderListWxVO orderListWxVO = orderService.list(marketUser.getId(),orderListWxBO);
       return BaseRespVo.ok(orderListWxVO);
    }

    //查看订单详情
    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId){

        OrderDetailWxVO orderDetailWxVO = orderService.detail(orderId);

        return BaseRespVo.ok(orderDetailWxVO);
    }


    //取消订单
    @PostMapping("cancel")
    public BaseRespVo cancel(@RequestBody MarketOrderGoods orderGoods){
       orderService.cancel(orderGoods.getOrderId());
       return BaseRespVo.ok();
    }

    //确认收货
    @PostMapping("confirm")
    public BaseRespVo confirm(@RequestBody MarketOrderGoods orderGoods){
       orderService.confirm(orderGoods.getOrderId());
       return BaseRespVo.ok();
    }

    //删除订单
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketOrderGoods orderGoods){
       orderService.delete(orderGoods.getOrderId());
       return BaseRespVo.ok();
    }

    //订单商品评价表
    @RequestMapping("goods")
    public BaseRespVo goods(MarketOrderGoods orderGoods){
       MarketOrderGoods orderGoods1 = orderService.getGoods(orderGoods.getOrderId(),orderGoods.getGoodsId());
       return BaseRespVo.ok(orderGoods1);
    }

    //订单评价
    @PostMapping("comment")
    public BaseRespVo comment(@RequestBody OrderCommentBO orderCommentBO){
       orderService.comment(orderCommentBO);
       return BaseRespVo.ok();
    }

    //申请退款
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody MarketOrderGoods marketOrderGoods){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();

       orderService.refund(marketUser.getId(),marketOrderGoods.getOrderId());
       return BaseRespVo.ok();
    }

}
